package com.cn.zkc3;

import org.I0Itec.zkclient.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenyuncong on 2019/4/1.
 */
public class TastChild extends ZkClient {

    private String masterNode = "/task-assist";
    private String nodeName ;
    private String child = masterNode + nodeName;
    private String checkNode = masterNode + "/check-node";
    private boolean checkHeartbeat = true;

    public void initNode(){
        child = masterNode + nodeName;
    }

    public TastChild(IZkConnection connection, String nodeName) {
        super(connection);
        this.nodeName = nodeName;
    }

    public TastChild(IZkConnection connection, int connectionTimeout, String nodeName) {
        super(connection, connectionTimeout);
        this.nodeName = nodeName;
    }

    public TastChild(String zkServers, int connectionTimeout, String nodeName) {
        super(zkServers, connectionTimeout);
        this.nodeName = nodeName;
    }

    public void init() {
        initNode();
        masterChildChange();
        childChanges();
        childDataChanges();
        newChildNode();
        checkHeartbeatChanges();
    }

    /**
     * 主节点和子节点 可以做上下线
     */
    public void masterChildChange() {
        this.subscribeChildChanges(masterNode, new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChild) throws Exception {
                System.out.println("--------------监听主节点 和 子节点 变化---------------");
                System.out.println("parentPath=" + parentPath);
                System.out.println("currentChild=" + currentChild);
            }
        });
    }

    /**
     * 子节点的监听
     */
    public void childChanges() {
        this.subscribeChildChanges(child, new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChild) throws Exception {
                System.out.println("--------------监听 子节点 变化---------------");
                System.out.println("parentPath=" + parentPath);
                System.out.println("currentChild=" + currentChild);
            }
        });
    }

    /**
     * 子节点数据监听
     */
    public void childDataChanges() {
        //监控主节 修改数据，和删除(subscribeChildChanges先运行然后再到subscribeDataChanges监听)
        this.subscribeDataChanges(child, new IZkDataListener() {
            //主节点数据修改
            @Override
            public void handleDataChange(String path, Object data) throws Exception {
                System.out.println("修改节点 path=" + path + ", value=" + data);
            }

            //主节点删除
            @Override
            public void handleDataDeleted(String path) throws Exception {
                System.out.println("删除的节点 path" + path);
            }
        });
    }

    /**
     * 新建一个子节点
     */
    public void newChildNode(){
        Tast tast = null;
        boolean bool = this.exists(child);
        if (!bool) {
            tast = new Tast(0, nodeName);
            this.createEphemeral(child, tast);
        }
    }

    /**
     * 心跳
     */
    public void checkHeartbeatChanges() {
        while (checkHeartbeat) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean checkBool = this.exists(checkNode);
            if (!checkBool) {
                this.createPersistent(checkNode, child);
            } else {
                this.writeData(checkNode, child);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        /*ZkClient zkClient = new ZkClient(new ZkConnection(TastAssist.connectString, 10000));
        String masterNode = "/task-assist";
        String nodeName = "/task-0";
        String child = masterNode + nodeName;
        String checkNode = masterNode + "/check-node";
        Tast tast = null;
        Thread.sleep(1000);
        tast = new Tast(1, nodeName);
        zkClient.writeData(child, tast);
        Thread.sleep(1000);*/

        TastChild tastChild=new TastChild(TastAssist.connectString, 10000,"/task-0");
        tastChild.init();


//        Thread thread=new Thread(() -> {
//            TastChild tastChild2=new TastChild(TastAssist.connectString, 10000,"/task-1");
//            tastChild2.init();
//        });
//        thread.start();
    }
}
